package DataStructures.HashTable;

/**
 * @author ：ALi
 * @date ：Created in 2021/11/21 14:28
 * @description：手写哈希表
 * @modified By：
 * @version: $
 */
public class HashTable {
    public static void main(String[] args) {
        Emp emp1 = new Emp(1, "tom1");
        Emp emp2 = new Emp(2, "tom2");
        Emp emp3 = new Emp(7, "tom2");
        HashTab ht = new HashTab(6);
        ht.add(emp1);
        ht.add(emp2);
        ht.add(emp3);
        ht.list();
        ht.findEmpById(22);
    }


}

//节点链表,只用来存储节点元素
class Emp {
    private int id;
    private String name;
    public Emp next;

    public Emp() {
    }

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "=>" +
                "id=" + id +
                ", name='" + name + '\''
                ;
    }
}

//接点管理链表。用来管理节点
class EmpLinkedList {
    private Emp head;

    public EmpLinkedList() {

    }

    /**
     * 增加链表
     *
     * @param emp 新增加的元素
     */
    public void add(Emp emp) {
        if (this.head == null) {
            head = emp;
            return;
        } else {
            Emp temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = emp;
        }
    }

    //展示链表信息
    public void list() {
        if (this.head == null) {
            System.out.println("空");
            return;
        }
        Emp temp = this.head;
        while (temp != null) {
            System.out.print(temp);
            temp = temp.next;
        }
        System.out.println();


    }

    //
    public Emp findEmpById(int id) {
        if (head == null) {
            return null;
        }
        Emp temp = this.head;
        while (temp != null) {
            if (temp.getId() == id) {
                return temp;
            } else {
                temp = temp.next;
            }
        }
        return null;
    }


}

/**
 * 哈希表，里面存储的是EmpLinkedList类型的链表，是用数组实现的。
 */
class HashTab {
    private EmpLinkedList[] empLinkedListArray;
    private int size;

    public HashTab(int size) {
        this.empLinkedListArray = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            this.empLinkedListArray[i] = new EmpLinkedList();
        }
        this.size = size;
    }

    /**
     * 添加元素
     *
     * @param emp
     */
    public void add(Emp emp) {
        int id = emp.getId();
        int index = id % size;
        //添加
        this.empLinkedListArray[index].add(emp);
    }

    /**
     * 展示链表
     */
    public void list() {
        if (this.size == 0) {
            System.out.println("为空");
        } else {
            for (int i = 0; i < this.size; i++) {
                System.out.print("第" + i + "条链表为：");
                this.empLinkedListArray[i].list();
            }
        }
    }

    public void findEmpById(int id) {

        if (this.size == 0) {
            System.out.println("链表为空");
            return;
        }
        Emp res = new Emp();
        int index = id % size;
        res = this.empLinkedListArray[index].findEmpById(id);
        if (res == null) {
            System.out.println("没有找到id为"+id+"的元素");
        }else {
            System.out.println("在第"+index+"条表中找到该雇员,id为"+id);
        }
    }

}